package cn.ecut.controller;

import cn.ecut.model.Contract;
import cn.ecut.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JMS
 * @date 2020/12/27 14:16
 */
@Controller
@SessionAttributes("contract")
public class ContractController {
    @Autowired
    ContractService contractService;

    @RequestMapping(value = "/insertContract")
    public String insertContract(@RequestParam("contract_num") String contract_num,
                                 @RequestParam("sub_branch") String sub_branch,
                                 @RequestParam("party_a") String party_a,
                                 @RequestParam("party_b") String party_b,
                                 @RequestParam("type_contract") String type_contract,
                                 @RequestParam("total_contract_amount") String total_contract_amount,
                                 @RequestParam("receivable_amount") String receivable_amount,
                                 @RequestParam("date_start") String date_start,
                                 @RequestParam("date_end") String date_end,
                                 @RequestParam("price") String price,
                                 @RequestParam("award_datetime") String award_datetime,
                                 @RequestParam("contract_status") String contract_status,
                                 @RequestParam("entry_datetime") String entry_datetime,
                                 HttpServletRequest request) {
        Contract contract = new Contract();
        contract.setContract_num(contract_num);
        request.setAttribute("contract_num",contract_num);
        contract.setSub_branch(sub_branch);
        contract.setParty_a(party_a);
        contract.setParty_b(party_b);
        contract.setType_contract(type_contract);
        contract.setTotal_contract_amount(total_contract_amount);
        contract.setReceivable_amount(receivable_amount);
        contract.setDate_start(date_start);
        contract.setDate_end(date_end);
        contract.setPrice(Integer.parseInt(price));
        contract.setAward_datetime(award_datetime);
        contract.setContract_status(contract_status);
        contract.setEntry_datetime(entry_datetime);
        if (contractService.insertContract(contract) == 1) {
            return "admin/updateApply";
        } else {
            return "user/contract";
        }
    }

    @RequestMapping("/getContractInfo")
    public String getContractInfo(@RequestParam("username") String username,
                                  HttpServletRequest request,
                                  Model model){
        request.getSession().removeAttribute("contract");
        Contract contract = contractService.getContractInfo(username);
        model.addAttribute("contract",contract);
        return "user/getRepair";
    }
}
